package qc.module.platform.mapper;

import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import qc.common.core.enums.ResourceStatusFlagEnum;
import qc.common.core.utils.DateUtil;
import qc.module.platform.dto.uac.UacUserInfoDto;
import qc.module.platform.dto.user.*;
import qc.module.platform.entity.QcUsers;

import java.util.Date;
import java.util.List;

/**
 * UserMapper
 *
 * @author QuCheng Tech
 * @create 2023/1/29
 */
@Mapper
public interface UserMapper {
    UserMapper MAPPER = Mappers.getMapper(UserMapper.class);

    @Mapping(target = "allowtime", source = "allowtm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "expiretime", source = "expiredtm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "order", source = "odr")
    @Mapping(target = "flag", source = "flag.index")
    UserInfoDto toDto(QcUsers en);


    @Mapping(target = "flag", source = "flag.index")
    List<UserInfoDto> toDtoList(List<QcUsers> list);


    @Mapping(target = "allowtime", ignore = true)
    @Mapping(target = "code", ignore = true)
    @Mapping(target = "project", ignore = true)
    @Mapping(target = "flag", ignore = true)
    @Mapping(target = "weixinid", ignore = true)
    @Mapping(target = "odr", ignore = true)
    List<UserSimpleInfoDto> toSimpDtoList(List<QcUsers> list);

    @Mapping(target = "allowtime", ignore = true)
    @Mapping(target = "code", ignore = true)
    @Mapping(target = "project", ignore = true)
    @Mapping(target = "flag", ignore = true)
    @Mapping(target = "weixinid", ignore = true)
    @Mapping(target = "odr", ignore = true)
    List<UserSimpleInfoDto> toInfoDtoSimpList(List<UserInfoDto> list);

    @Mapping(target = "project", ignore = true)
    UacUserInfoDto toUserInfoDto(QcUsers en);


    @Mapping(target = "allowtime", source = "allowtm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "expiretime", source = "expiredtm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "order", source = "odr")
    @Mapping(target = "flag", source = "flag.index")
    UsersDto toUserDto(QcUsers en);


    @Mapping(target = "allowtm", source = "allowtime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "expiredtm", ignore = true)
    @Mapping(target = "odr", source = "order")
    @Mapping(target = "flag", ignore = true)
    QcUsers toUserEntity(UserEditDto en);


    @Mapping(target = "allowtm", source = "allowtime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "expiredtm", ignore = true)
    @Mapping(target = "id", ignore = true)
    @Mapping(target = "flag", ignore = true)
    QcUsers toEntity(UserAddDto dto);


    //转化为枚举类型字段
    default ResourceStatusFlagEnum toEnum(Integer flag) {
        for (ResourceStatusFlagEnum eu : ResourceStatusFlagEnum.values()) {
            if (eu.getIndex() == flag) {
                return eu;
            }
        }
        return null;
    }


    default List<UserInfoDto> toInfoDtoList(List<QcUsers> en) {
        List<UserInfoDto> userInfoDtoList = MAPPER.toDtoList(en);
        for (UserInfoDto userInfoDto : userInfoDtoList)
            //手机号脱敏
            if (!StringUtils.isBlank(userInfoDto.getPhone())) {
                userInfoDto.setPhone(userInfoDto.getPhone().replaceAll("(\\w{3})\\w*(\\w{4})", "$1****$2"));
            }

        return userInfoDtoList;
    }

    default List<UserSimpleInfoDto> toInfoDtoSimpleList(List<UserInfoDto> en) {
        List<UserSimpleInfoDto> userSimpleInfoDtos = MAPPER.toInfoDtoSimpList(en);
        for (UserSimpleInfoDto userSimpleInfoDto : userSimpleInfoDtos)
            //手机号脱敏
            if (!StringUtils.isBlank(userSimpleInfoDto.getPhone())) {
                userSimpleInfoDto.setPhone(userSimpleInfoDto.getPhone().replaceAll("(\\w{3})\\w*(\\w{4})", "$1****$2"));
            }

        return userSimpleInfoDtos;
    }


    default List<UserSimpleInfoDto> toSimpDtoLists(List<QcUsers> en) {
        List<UserSimpleInfoDto> userSimpleInfoDtos = MAPPER.toSimpDtoList(en);
        for (UserSimpleInfoDto userSimpleInfoDto : userSimpleInfoDtos)
            //手机号脱敏
            if (!StringUtils.isBlank(userSimpleInfoDto.getPhone())) {
                userSimpleInfoDto.setPhone(userSimpleInfoDto.getPhone().replaceAll("(\\w{3})\\w*(\\w{4})", "$1****$2"));
            }

        return userSimpleInfoDtos;
    }


    default UacUserInfoDto toUacUserInfoDto(QcUsers en) {
        UacUserInfoDto dto = MAPPER.toUserInfoDto(en);

        //手机号脱敏
        if (!StringUtils.isBlank(en.getPhone())) {
            dto.setPhone(en.getPhone().replaceAll("(\\w{3})\\w*(\\w{4})", "$1****$2"));
        }

        return dto;
    }


    /**
     * 状态码转换
     *
     * @param dto:
     * @return QcUsers
     * @author QcCheng Tech
     * @date 2023/7/6
     */

    default QcUsers UserEditDtoToEntity(UserEditDto dto) {
        //DTO转换为Entity
        QcUsers en = UserMapper.MAPPER.toUserEntity(dto);

        //处理标记，非空时进行转换，空值时使用默认
        Integer flag = null;
        if (dto.getFlag() > 0x0) {
            try {
                flag = (int) dto.getFlag();
            } catch (NumberFormatException exception) {
                //转换错误不处理，使用默认状态
            }
        }
        en.setFlag(getResourceStatusFlagEnum(flag));

        //判断过期时间
        if (!StringUtils.isBlank(dto.getExpiretime())) {
            Date expireTime = DateUtil.parseDate(dto.getExpiretime());
            en.setExpiredtm(expireTime);
        }

        return en;
    }


    /**
     * 增加用户状态码转换
     *
     * @param dto: 增加用户信息
     * @return QcUsers 用户信息
     * @author QcCheng Tech
     * @date 2023/7/6
     */
    default QcUsers UserAddDtoToEntity(UserAddDto dto) {
        //DTO转换为Entity
        QcUsers en = UserMapper.MAPPER.toEntity(dto);

        //处理标记，非空时进行转换，空值时使用默认
        Integer flag = null;
        if (dto.getFlag() > 0x0) {
            try {
                flag = (int) dto.getFlag();
            } catch (NumberFormatException exception) {
                //转换错误不处理，使用默认状态
            }
        }
        en.setFlag(getResourceStatusFlagEnum(flag));

        //判断过期时间
        if (!StringUtils.isBlank(dto.getExpiretime())) {
            Date expireTime = DateUtil.parseDate(dto.getExpiretime());
            en.setExpiredtm(expireTime);
        }

        return en;
    }

    static ResourceStatusFlagEnum getResourceStatusFlagEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (ResourceStatusFlagEnum item : ResourceStatusFlagEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }
        return ResourceStatusFlagEnum.NORMAL;
    }


}
